package com.jrx.anytxn.investor.timer.job.transfer.config;

import com.jrx.anytxn.investor.entity.TlFundTransferLog;
import com.jrx.anytxn.investor.timer.config.AnytxnBatchProperties;
import com.jrx.anytxn.investor.timer.job.transfer.step.TransferProcessor;
import com.jrx.anytxn.investor.timer.job.transfer.step.TransferReader;
import com.jrx.anytxn.investor.timer.job.transfer.step.TransferWriter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * 放款任务
 *
 * @author jing.xy
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class TransferConfiguration {
    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;


    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;

    @Autowired
    DataSource dataSource;

    @Autowired
    private PlatformTransactionManager platformTransactionManager;


    @Bean
    public Job transferJob() {
        return jobBuilderFactory.get("transferJob")
                .start(transferStep())
                .build();
    }

    @Bean
    public Step transferStep() {
        return stepBuilderFactory.get("transferStep")
                .<TlFundTransferLog,TlFundTransferLog>chunk(anytxnBatchProperties.getTransferConfig().getChunkLimit())//表示一批处理100条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(transferReader())
                .processor(transferProcessor())
                .writer(transferWriter())
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getTransferConfig().getThrottleLimit())//多线程
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<TlFundTransferLog> transferReader() {
        //同步reader   线程安全
        SynchronizedItemStreamReader<TlFundTransferLog> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        TransferReader transferReader = new TransferReader(sqlSessionFactory);
        transferReader.setSaveState(false);
        synchronizedItemStreamReader.setDelegate(transferReader);
        return synchronizedItemStreamReader;
    }

    @Bean
    @StepScope
    public TransferProcessor transferProcessor() {
        TransferProcessor transferProcessor = new TransferProcessor();
        return transferProcessor;
    }


    @Bean
    @StepScope
    public ItemWriter<TlFundTransferLog> transferWriter() {
        return new TransferWriter();
    }

}